<template>
  <div class="rated">
    <p class="title">最受好评电影</p>
    <div class="roll_x">
      <ul>
        <li v-for="item in ratedList" :key="item._id">
          <div class="imgwrap">
            <img :src="item.imgUrl" alt="" />
            <p v-if="item.score">观众评分:{{ item.score }}</p>
            <p v-else>{{ item.wishNum }}人想看</p>
          </div>
          <p class="item-title">{{ item.title }}</p>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
// import http from "../../utils/api";
// import { getRatedDataApi } from "@/utils/api";
import { mapState, mapActions } from "vuex";

// 引入better-scroll插件，用于优化滚动和上拉加载功能
import BetterScroll from "better-scroll";

export default {
  data() {
    return {
      // ratedList: [],
    };
  },
  created() {
    // this.getRatedData();
    this.getRatedDataAsync().then(async () => {
      await this.$nextTick();
      new BetterScroll(".roll_x", {
        scrollX: true,
        scrollY: false,
        click: true,
      });
    });
    console.log(this.ratedList);
  },
  // computed: mapState(["ratedList"]),
  computed: mapState({
    ratedList: (state) => state.rated.ratedList,
  }),
  methods: {
    ...mapActions(["getRatedDataAsync"]),
    // async getRatedData() {
    // 使用fetch来请求数据列表
    // fetch("http://www.pudge.wang:3080/api/rated/list")
    //   .then((response) => response.json())
    //   .then((res) => {
    //     if (res.status === 0) {
    //       this.ratedList = res.result;
    //     }
    //   });
    // Promise写法 -- 利用封装的http
    // http.get("/rated/list").then((res) => {
    //   this.ratedlist = res.list;
    // });
    // async写法 -- 在promise基础上改进，并进一步封装api对象进一步简化请求操作代码
    // const res = await getRatedDataApi();
    // this.ratedList = res.result;
    // 添加better-scroll，条件:必须在数据渲染完以后，第一个参数是容器名，第二个参数是选项对象
    // 等待页面渲染完以后在设置better-scroll对象，由于Vue在更新DOM时是异步执行的
    //   await this.$nextTick();
    //   new BetterScroll(".roll_x", {
    //     scrollX: true,
    //     scrollY: false,
    //     click: true,
    //   });
    // },
  },
};
</script>

<style lang="less" scoped>
@import "../../assets/css/commons.less";
.rated {
  width: 100%;
  height: 200px;
  background: #fff;
  padding: 12px 15px;
  margin-bottom: 10px;

  .title {
    margin: 0;
    font-size: @fontSize_14;
    color: #333;
    margin-bottom: 12px;
  }

  .roll_x {
    width: 100%;
    height: 144px;
    overflow: hidden;
  }
  ul {
    // 转化成行内弹性盒，理由是使用better-scroll时子元素宽度必须比父元素(容器)大，才能实现滚动效果，如果是设置为flex因为ul是块状元素，与父元素同宽无法达到这一要求
    display: inline-flex;
    font-size: @fontSize_14;

    li {
      width: 85px;
      height: 140px;
      flex-shrink: 0;
      margin-right: 10px;

      .imgwrap {
        width: 85px;
        height: 115px;
        position: relative;
        img {
          width: 100%;
          height: 100%;
        }
        p {
          width: 100%;
          line-height: 24px;
          text-align: center;
          background: linear-gradient(
            to top,
            rgba(0, 0, 0, 0.8),
            rgba(0, 0, 0, 0.8)
          );
          position: absolute;
          left: 0;
          bottom: 0;
          color: #faaf00;
          font-weight: 700;
          font-size: @fontSize_10;
        }
      }

      .item-title {
        width: 100%;
        text-align: center;
        line-height: 30px;
        .overflow_hidden();
        font-weight: 700;
      }
    }
  }
}
</style>
